<?php
                                                                                                                                                                                                                                                                                                                                        
// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      
                                                                                                                                                                     
//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2021/10/09
//      |_|                                         | TpMeCMS

namespace app\api\controller;
use app\api\controller\Tpmecms;
use think\Db;
use think\Config;



/**
 * 支付宝提现💰
 * @ApiInternal()
 */
class AlipayOut extends Tpmecms
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 支付宝提现💰
     *
     * @param array $data 订单信息
     * @return void
     */
    public function alipay_out_money($data=null)
    {
        $params = $this->get_alipay_params();

        // SDK实例化，传入公共配置
        $pay = new \Yurun\PaySDK\AlipayApp\SDK($params);
        // 支付接口
        $request = new \Yurun\PaySDK\AlipayApp\Fund\Transfer\Request;

        $request->businessParams->out_biz_no = $data['order'];
        $request->businessParams->payee_type = 'ALIPAY_LOGONID';
        $request->businessParams->payee_account = $data['alipay_account'];//'496631085@qq.com'
        $request->businessParams->amount = $data['money'];
        // 调用接口
        $result = $pay->execute($request);
        if($pay->checkResult()){
            $up_data['pay_time'] = strtotime($result['alipay_fund_trans_toaccount_transfer_response']['pay_date']);
            $up_data['pay_status'] = '2';//支付状态:1=待到账,2=已到账,3=已失败,4=已超时

            Db::name('money_out')->where('order',$data['order'])->update($up_data);
            $this->DecUserMoney($data['uid'],$data['money'],'支付宝提现');
            $this->success('提现成功',$result);
        }else{
            $this->error($pay->getError(),$pay->getErrorCode());
        }
        var_dump('result:', $result);
        var_dump('success:', $pay->checkResult());
        var_dump('error:', $pay->getError(), 'error_code:', $pay->getErrorCode());



        // "alipay_fund_trans_toaccount_transfer_response": {
        //     "code": "10000",
        //     "msg": "Success",
        //     "order_id": "20211009110070001506970040266278",
        //     "out_biz_no": "test97237223",
        //     "pay_date": "2021-10-09 17:35:25"
        //     },
        //     "alipay_cert_sn": "ea6ff5acb799eff108b289cf922db9bd",
        //     "sign": "eO+DjUy3qrnAcWoykrLFbaq2NrlIqJpbo5wgkEzhlGcnUGp5VJLksrqMBL7baJBpRLoLdLbugINALovsekQ3EVEdThMv9V6w3lu0wUIj90qTTaan4OyddW62N+76XzdILjA0BWw9vDtpjsd/KHXSn2MEVkUpM7kpk/4+V+s+iZkd8JcGiH9gixiTd0/gHvb6aSC4KP8IaKcsgY23riHXeZ5EX2gOZ7IrTKc04lKHIDUbVgPJdI+2nat/BsEcdm4e0eDhMR1oG7Dz51lAA9EUAiy99U5/DD437Nqnr1TmTstWMvtg+C85JrrSJSDXUVR75TFebT4/ZdNxnd3e4WzOAg=="

        
        // string(7) "result:" array(3) { ["alipay_fund_trans_toaccount_transfer_response"]=> array(5) { ["code"]=> string(5) "10000" ["msg"]=> string(7)
        // "Success" ["order_id"]=> string(32) "20211009110070001506970040437135" ["out_biz_no"]=> string(12) "test18728846" ["pay_date"]=> string(19) "2021-10-09 17:28:05" } ["alipay_cert_sn"]=> string(32) "ea6ff5acb799eff108b289cf922db9bd" ["sign"]=> string(344) "YtbNI2zSekjtRdyJXB7OD9j3WmxquvqvAstaqNWCQb3eBp9XSQBCvz/uDvv159JAqeukVwyKRgy15oIGmBsy4mqB8XwDlXcVzyqlGtu5Yg+4XNIkSpVvT2DK5eWehSgRemVbruvWoUjKKiyfyEqqOlyEe0lkKs0d6fzZ6VeIbRCTNSXPQgWfavgZO8utK20cgtOqSgfkjv+aHxHtaWvqr78DcYOaJLjOWU1W9BoESmMZ+ykOElcEKcttOEk5wy9a1o8W4UYp8YueyI4j49QLb9ZYiqBF0gSD8jBkHOEBrX3ykJScBWukmuWYwrCr+9FMAdlSedaJ4bLso6E7KEQDlA==" } 
        // string(8) "success:" bool(true) string(6) 
        // "error:" string(0) "" string(11) "error_code:" string(0) ""
    }

}